<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .content div {
      height: 400px;
    }
    .drag {
      width: 400px;
      height: 400px;
      background-color: aliceblue;
      position: absolute;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="content">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
    <div class="drag"></div>
  </div>
  <div class="last">......loading.......</div>
  <script>
    const last = document.querySelector('.last');
    const content = document.querySelector('.content');
    const drag = document.querySelector('.drag');
    function append() {
      setTimeout(() => {
        for (let index = 0; index < 10; index++) {
          let div = document.createElement('div');
          div.innerHTML = index + 1;
          content.appendChild(div);
        } 
        isAdd = false;
      }, 1000);
    }
    let isAdd = false;
    
    const io = new IntersectionObserver((event) => {
      // console.log(event);
      event.forEach(ev => {
        
        console.log(ev.intersectionRatio);
        if(ev.intersectionRatio > 0) {
          if(isAdd) {
            return;
          }
          isAdd = true;
          append();
        }
      })
    })
    // , {
    //   threshold: [0, 0.25, 0.5, 0.75, 1]
    // }
    io.observe(last)
    // document.addEventListener('scroll', event => {
    //   // console.log(last.getBoundingClientRect())
    //   // if(document.documentElement.scrollTop + document.documentElement.clientHeight >= last.getBoundingClientRect().top) {
    //   if(last.getBoundingClientRect().top <= document.documentElement.clientHeight - last.clientHeight) {
    //     if(isAdd) {
    //       return;
    //     }
    //     isAdd = true;
    //     console.log('look');
    //     append();
    //   }
    // })
    // const divs = document.querySelectorAll('div');
    // divs.forEach(div => {
    //   div.addEventListener('click', event => {
    //     console.log(event.target.offsetTop);
    //     console.log(event.target.clientTop);
    //     // // 当前鼠标点击位置据浏览器左侧头部 距离
    //     // console.log('client', event.clientX, event.clientY);
    //     // // 当前鼠标点击位置据文档最左侧 头部 距离 （包括滚动条）
    //     // // event.pageX = event.clientX + 横向滚动条隐藏的长度
    //     // // event.pageY = event.clientY + 纵向滚动条隐藏的长度
    //     // console.log('page', event.pageX, event.pageY);
    //     // // 当前鼠标点击位置据屏幕最左侧 头部的距离 
    //     // console.log('screen', event.screenX, event.screenY);
    //   })
    // })
    
    drag.addEventListener('mousedown', function(ev) {
      let offsetTop = ev.clientY - (drag.offsetTop - document.documentElement.scrollTop);
      let offsetLeft = ev.clientX - (drag.offsetLeft - document.documentElement.scrollLeft);
      function dragfn(ev) {
          drag.style.left = ev.clientX - offsetLeft + document.documentElement.scrollLeft + 'px';
          drag.style.top = ev.clientY - offsetTop + document.documentElement.scrollTop + 'px';
      }
      document.addEventListener('mousemove', dragfn)
      document.addEventListener('mouseup', function() {
        document.removeEventListener('mousemove', dragfn)
      })
    })
  </script>
</body>
</html>